package com.example.my_musicbox;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity implements View.OnClickListener {
    //添加日志的TAG常量
    private static final String TAG = "MainActivity";

    // 获取界面中显示歌曲图片，歌曲标题、作者文本框
    ImageView song_pic;
    TextView song, singer;
    // 播放、暂停、停止、上一首、下一首按钮
    ImageButton play, stop, front, next;
    //activityReceiver用于监听Service传回的广播
    ActivityReceiver activityReceiver;

    public static final String CTL_ACTION = "org.wjw.action.CTL_ACTION";
    public static final String UPDATE_ACTION = "org.wjw.action.UPDATE_ACTION";

    // 定义音乐的播放状态，1代表没有播放；2代表正在播放；3代表暂停
    int status = 1;

    int[] song_pics = new int[]{R.drawable.mojito, R.drawable.secret_base, R.drawable.i_want_to_love_this_world};
    String[] songs = new String[]{"Mojito", "secret base ～君がくれたもの～", "この世界を愛したい"};
    String[] singers = new String[]{"周杰伦", "茅野愛衣 (かやの あい)、戸松遥 (とまつ はるか)、早见沙织 (はやみ さおり)", "鹿乃 (かの)"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取程序界面界面中的两个按钮
        front = (ImageButton) this.findViewById(R.id.front);
        play = (ImageButton) this.findViewById(R.id.play);
        stop = (ImageButton) this.findViewById(R.id.stop);
        next = (ImageButton) this.findViewById(R.id.next);

        // 获取歌曲名、歌手名
        song_pic = (ImageView) findViewById(R.id.song_pic);
        song = (TextView) findViewById(R.id.song);
        singer = (TextView) findViewById(R.id.singer);

        // 为两个按钮的单击事件添加监听器
        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        front.setOnClickListener(this);
        next.setOnClickListener(this);

        activityReceiver = new ActivityReceiver();
        // 创建IntentFilter
        IntentFilter filter = new IntentFilter();
        // 指定BroadcastReceiver监听的Action
        filter.addAction(UPDATE_ACTION);
        // 注册BroadcastReceiver
        registerReceiver(activityReceiver, filter);

        Intent intent = new Intent(this, MusicService.class);
        // 启动后台Service
        startService(intent);

        //打印日志信息
        Log.d(TAG, "MainActivity调用onCreate()...");
    }

    // 自定义的BroadcastReceiver，负责监听从Service传回来的广播
    public class ActivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 获取Intent中的update消息，update代表播放状态
            int update = intent.getIntExtra("update", -1);
            // 获取Intent中的current消息，current代表当前正在播放的歌曲
            int current = intent.getIntExtra("current", -1);
            if (current >= 0) {
                song_pic.setImageResource(song_pics[current]);
                song.setText(songs[current]);
                singer.setText(singers[current]);
            }

            //根据点击情况切换状态
            switch (update) {
                // 控制系统进入停止状态
                case 0x11:
                    play.setImageResource(R.drawable.play);
                    status = 1;
                    break;

                // 控制系统进入播放状态
                case 2:
                    // 播放状态下设置使用暂停图标
                    play.setImageResource(R.drawable.pause);
                    // 设置当前状态
                    status = 2;
                    break;

                // 控制系统进入暂停状态
                case 0x13:
                    // 暂停状态下设置使用播放图标
                    play.setImageResource(R.drawable.play);
                    // 设置当前状态
                    status = 3;
                    break;
            }

            //打印日志信息
            Log.d(TAG, "MainActivity调用onReceive()...");
        }
    }

    //销毁广播
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(activityReceiver);

        //打印日志信息
        Log.d(TAG, "MainActivity调用onDestroy()...");
    }

    @Override
    public void onClick(View source) {
        // 创建Intent
        Intent intent = new Intent("org.wjw.action.CTL_ACTION");
        switch (source.getId()) {
            case R.id.play:
                // 按下播放/暂停按钮
                intent.putExtra("control", 1);
                break;
            case R.id.stop:
                // 按下停止按钮
                intent.putExtra("control", 2);
                break;
            case R.id.front:
                //按上一首
                intent.putExtra("control", 3);
                break;
            case R.id.next:
                //按下一首
                intent.putExtra("control", 4);
                break;
        }
        // 发送广播，将被Service组件中的BroadcastReceiver接收到
        sendBroadcast(intent);
    }
}